﻿#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Neat;
using Neat.Mathematics;
using Neat.GUI;
using Neat.Graphics;
using Sectors;
using Microsoft.Xna.Framework.Input;
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
#endregion

namespace SectorsDesigner
{
    public partial class SectorsMapMakerControl : GraphicsDeviceControl
    {
        Vector2 GridSize = new Vector2(10f);
        Vector2 GetMousePosition(bool fake) //Does snapping.
        {
            //Shitty algorithm, needs fixing ASAP.
            var M = fake ? MousePosition - offset : MousePosition;

            for (int i = 0; i < Vertices.Count; i++)
            {
                if (DeletedVertices.Contains(i)) continue;

                if (Vector2.Distance(M, Vertices[i]) <= WallSelectRadius * 1.75f)
                    return Vertices[i];

                LineSegment lv1 = new LineSegment(Vertices[i], 0);
                LineSegment lv2 = new LineSegment(Vertices[i], MathHelper.PiOver2);
                Vector2 p1 = GeometryHelper.GetShortestVectorToLine(lv1, M, false);
                if (Vector2.Distance(p1, M) <= WallSelectRadius)
                    return new Vector2(SnapToGrid(p1).X, p1.Y);
                p1 = GeometryHelper.GetShortestVectorToLine(lv2, M, false);
                if (Vector2.Distance(p1, M) <= WallSelectRadius)
                    return new Vector2(p1.X,SnapToGrid(p1).Y);

            }

            for (int i = 0; i < Edges.Count; i++)
            {
                if (DeletedEdges.Contains(i)) continue;
                foreach (var seg in Edges[i].ToSegments())
                {
                    Vector2 p = GeometryHelper.GetShortestVectorToLine(seg, M, true);
                    if (Vector2.Distance(p, M) <= WallSelectRadius)
                    {
                        return p;
                    }
                }
            }
            
            return SnapToGrid(M);
        }

        Vector2 SnapToGrid(Vector2 v)
        {
            if (!DoSnapToGrid) return v;
            if (EditMode != EditModes.NewSector) return v;
            v.X = (int)(v.X / GridSize.X);
            v.Y = (int)(v.Y / GridSize.Y);
            return v * GridSize;
        }
        public void MouseClicked(MouseEventArgs e)
        {
            MousePosition = new Vector2(e.X, e.Y);
            switch (EditMode)
            {
                case EditModes.NewSector:
                    if (e.Button == System.Windows.Forms.MouseButtons.Left)
                        NewSectorAddVertex(GetMousePosition(true));
                    else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                        NewSectorBackOff(); //TODO: Needs fixing
                    else if (e.Button == System.Windows.Forms.MouseButtons.Middle)
                        NewSectorFinish();
                    break;

                case EditModes.SelectEdge:
                    if (e.Button == System.Windows.Forms.MouseButtons.Left)
                        SelectNearestWall(MousePosition - offset);
                    else if (e.Button == System.Windows.Forms.MouseButtons.Middle)
                        SplitWalls(GetMousePosition(true), false);
                    else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    {
                        SelectNearestWall(GetMousePosition(false));
                        ReverseSelected();
                    }
                    break;

                case EditModes.SelectSector:
                    if (e.Button == System.Windows.Forms.MouseButtons.Left)
                        SelectSector(MousePosition - offset);
                    else if (e.Button == System.Windows.Forms.MouseButtons.Middle) ;
                    else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    {
                        SelectSector(MousePosition - offset);
                        ReverseSelected();
                    }
                    break;

                case EditModes.SelectVertex:
                    if (e.Button == System.Windows.Forms.MouseButtons.Left)
                        SelectVertex(MousePosition-offset);
                    else if (e.Button == System.Windows.Forms.MouseButtons.Middle)
                        SplitWalls(GetMousePosition(true), true);
                    else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    {
                        SelectVertex(GetMousePosition(false));
                        ReverseSelected();
                    }
                    //Mouse-drag right: move vertex
                    break;


                case EditModes.SplitEdge: //no need to have this mode. will delete it later.
                    if (e.Button == System.Windows.Forms.MouseButtons.Left)
                    {
                        SelectNearestWall(MousePosition-offset);
                        SplitWalls(GetMousePosition(true), false);
                    }
                    else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    {
                        SelectNearestWall(MousePosition-offset);
                        MergeWalls();
                    }
                    break;
            }
        }

        public void MouseDowned(MouseEventArgs e)
        {
        }
    }
}